package com.scalar.chiptrack.adaptor.chartered;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;
import java.io.FileReader;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: ChandraSekhar
 * Date: Aug 30, 2006
 * Time: 4:40:04 AM
 */
public class CharteredHtmlShipParser extends HTMLEditorKit.ParserCallback
{
    private String m_Number = null;
    private CharteredShipStatus shipStatus = null;
    private ArrayList m_shipList = new ArrayList();
    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;

    public CharteredHtmlShipParser()
    {
         m_tableTagCount = 0;
         m_readyForParsing = false;
         m_cellOpen = false;
         m_cellIndex = -1;
         m_invalidRow = false;
    }

    public void parse(Reader reader) throws Exception
    {
        DTD dtd = DTD.getDTD("HTML");
        DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);

    }

    public ArrayList parse( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < m_shipList.size(); i++ )
        {
            shipStatus = ( CharteredShipStatus ) m_shipList.get( i );

        }
        return m_shipList;
    }

    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {

        // FOR PARSING THE DOWNLOADED WIP STATUS FILE
        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;
            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 13 )
            {
                m_readyForParsing = true;
            }
            else if ( m_tableTagCount == 14 )
            {
                //this block is entered when the table containing data is parsed
                if ( ( shipStatus != null ) && ( m_invalidRow == false ) )
                {
                    m_shipList.add( shipStatus );
                }

                m_readyForParsing = false;
            }
        }

        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ) && ( a.getAttributeCount() == 0 ) )
            {

                if ( ( shipStatus != null ) )
                {
                    m_shipList.add( shipStatus );
                }

                shipStatus = new CharteredShipStatus();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }
    }

    public void handleText( char[] data, int pos )
    {                   
        if ( ( m_readyForParsing == false ) || (shipStatus == null ) )
        {
            return;
        }

        String text = new String( data );

        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();
        }

        switch ( m_cellIndex )
        {

            case 0:

                if ( !( "".equals( text ) ) && !( "Ship Date".equals( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setShipDate(text);
                }
                break;

            case 1:
                if ( !( "".equals( text ) ) && !( "Invoice #".equals( text ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setInvoiceNo( text );
                }
                break;

            case 2:

                if ( !( "".equals( text ) ) && !( "Invoice Date".equalsIgnoreCase( text ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setInvoiceDate( text );
                }

                break;

            case 3:
                if ( !( "".equals( text ) ) && !( "PO#".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setCustPo( text );
                }
                break;

            case 4:
                if ( !( "".equals( text ) ) && !( "SO#".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    shipStatus.setSoNO( text );
                }
                break;

            case 5:
                if ( !( "".equals( text ) ) && !( "Process Name".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setProcessName( text );
                }
                break;

            case 6:
               if ( !( "".equals( text ) ) && !( "Customer Partname".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
               {
                   shipStatus.setCustPartNo( text );
               }
               break;

            case 7:
                if ( !( "".equals( text ) ) && !( "Chartered Part Name".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setVendorPartNo( text );
                }
                break;

            case 8:
                if ( !( "".equals( text ) ) && !( "Lot Id".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setCustomerLot( text );
                }
                break;

            case 10:
                if ( !( "".equals( text ) ) && !( "LOT TYPE".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setLotType( text );
                }
                break;

            case 11:
                if ( !( "".equals( text ) ) && !( "Lot Priority".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setLotPriority( text );
                }
                break;

            case 12:
                if ( !( "".equals( text ) ) && !( "Calculated GDPW".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setGrossDPW( text );
                }
                break;

            case 13:
                if ( !( "".equals( text ) ) && !( "Calculated GDPL".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setGorssDPL( text );
                }
                break;

            case 14:
                if ( !( "".equals( text ) ) && !( "Cycle Time".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setCycleTime( text );
                }
                break;

            case 15:
                if ( !( "".equals( text ) ) && !( "Bill Qty (Wfr)".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setShipWaferQty( text );
                }
                break;

            case 16:
                if ( !( "".equals( text ) ) && !( "Wfr Ids".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setWaferID( text );
                }
                break;

            case 17:
                if ( !( "".equals( text ) ) && !( "Bill Qty (Die)".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setShipDieQty( text );
                }
                break;

            case 18:
                if ( !( "".equals( text ) ) && !( "Ship To Location".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setShipToLocation( text );
                }
                break;

            case 19:
                if ( !( "".equals( text ) ) && !( "Bill To Location".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setBilToLocation( text );
                }
                break;

            case 20:
                if ( !( "".equals( text ) ) && !( "ETA".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setEtaDate( text );
                }
                break;

            case 21:
                if ( !( "".equals( text ) ) && !( "ETD".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setEtdDate( text );
                }
                break;

            case 22:
                if ( !( "".equals( text ) ) && !( "Forwarder".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setFarworder( text );
                }
                break;

            case 23:
                if ( !( "".equals( text ) ) && !( "HAWB".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    shipStatus.setHawbNo( text );
                }
                break;

            case 24:
                if ( !( "".equals( text ) ) && !( "MAWB".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setMawbNo( text );
                }
                break;
            case 25:
                if ( !( "".equals( text ) ) && !( "Flight #".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setFlightNo( text );
                }
                break;
            case 26:
                if ( !( "".equals( text ) ) && !( "Connecting Flight #".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    shipStatus.setConnFlightNo( text );
                }
                break;
           default:
                break;
        }

    }

    // Resets the objects state
    public void reset()
    {
        m_tableTagCount = 0;
        m_readyForParsing = false;
        m_cellOpen = false;
        m_cellIndex = -1;
        m_invalidRow = false;
    }

    public String getNumber() {
        return m_Number;
    }

    public void setNumber(String m_Number) {
        this.m_Number = m_Number;
    }
}

